<template>
  <div class="admin-container">
    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 搜索区域 -->
      <div class="search-container">
        <div class="search-title">
          <i class="fas fa-search"></i> 管理员信息查询
        </div>
        <div class="search-form">
          <div class="form-item">
            <label><i class="fas fa-user"></i> 姓名</label>
            <input type="text" placeholder="请输入姓名" v-model="searchForm.name">
          </div>
          <div class="form-item">
            <label><i class="fas fa-user-tag"></i> 角色</label>
            <input type="text" placeholder="请输入角色" v-model="searchForm.role">
          </div>
          <div class="form-item">
            <label><i class="fas fa-toggle-on"></i> 状态</label>
            <select v-model="searchForm.status">
              <option value="">全部</option>
              <option value="1">正常</option>
              <option value="0">停用</option>
            </select>
          </div>
          <div class="button-group">
            <button class="search-btn" @click="handleSearch">
              <i class="fas fa-search"></i> 查询
            </button>
            
            <button class="reset-btn" @click="resetSearch">
              <i class="fas fa-redo"></i> 重置
            </button>
          </div>
        </div>
      </div>
      
      <!-- 表格区域 -->
      <div class="table-container">
        <div class="table-header">
          <h3><i class="fas fa-list"></i> 管理员信息列表</h3>
          <div class="table-total">共 {{ total }} 条记录</div>
        </div>
        
        <table class="admin-table" v-if="adminList.length">
          <thead>
            <tr>
              <th>序号</th>
              <th>姓名</th>
              <th>角色</th>
              <th>部门</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in adminList" :key="index">
              <td>{{ (currentPage - 1) * pageSize + index + 1 }}</td>
              <td>{{ item.name || '-' }}</td>
              <td>{{ item.role }}</td>
              <td>{{ item.department }}</td>
              <td :class="item.status === 1 ? 'status-active' : 'status-inactive'">
                {{ item.status === 1 ? '正常' : '停用' }}
              </td>
              <td>
                <button class="action-btn detail-btn" @click="showAdminDetail(item)" :style="{ marginRight: '20px' }">
                  <i class="fas fa-eye"></i> 查看
                </button>
                <button class="action-btn edit-btn" @click="openEditModal(item)">
                  <i class="fas fa-edit"></i> 编辑
                </button>
              </td>
            </tr>
          </tbody>
        </table>
        
        <div class="empty-data" v-else>
          <i class="fas fa-database"></i>
          <p>暂无管理员数据</p>
        </div>
        
        <div class="pagination" v-if="total > pageSize">
          <button @click="goToPage(1)" :disabled="currentPage === 1">首页</button>
          <button @click="goToPage(currentPage - 1)" :disabled="currentPage === 1">上一页</button>
          <button 
            v-for="page in visiblePages" 
            :key="page"
            @click="goToPage(page)"
            :class="{active: currentPage === page}"
          >
            {{ page }}
          </button>
          <button @click="goToPage(currentPage + 1)" :disabled="currentPage === totalPages">下一页</button>
          <button @click="goToPage(totalPages)" :disabled="currentPage === totalPages">末页</button>
        </div>
      </div>
      
      <!-- 管理员详情对话框 -->
      <div class="modal" v-if="showDetailModal">
        <div class="modal-content">
          <div class="modal-header">
            <h3>管理员详情</h3>
            <button class="close-btn" @click="showDetailModal = false">
              <i class="fas fa-times"></i>
            </button>
          </div>
          <div class="modal-body">
            <el-descriptions
              class="margin-top"
              title="管理员信息"
              :column="2"
              direction="vertical"
              border
            >
              <el-descriptions-item label="姓名">{{ currentDetail.name }}</el-descriptions-item>
              <el-descriptions-item label="角色">{{ currentDetail.role }}</el-descriptions-item>
              <el-descriptions-item label="部门">{{ currentDetail.department }}</el-descriptions-item>
              <el-descriptions-item label="状态">
                <el-tag :type="currentDetail.status === 1 ? 'success' : 'danger'" size="small">
                  {{ currentDetail.status === 1 ? '正常' : '停用' }}
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="权限" :span="2">{{ currentDetail.permissions }}</el-descriptions-item>
            </el-descriptions>
          </div>
          <div class="modal-footer">
            <button class="modal-close-btn" @click="showDetailModal = false">关闭</button>
          </div>
        </div>
      </div>
      
      <!-- 管理员编辑对话框 -->
      <div class="modal" v-if="showEditModal">
        <div class="modal-content">
          <div class="modal-header">
            <h3>修改管理员信息</h3>
            <button class="close-btn" @click="closeEditModal">
              <i class="fas fa-times"></i>
            </button>
          </div>
          <div class="modal-body">
            <div class="form-item">
              <label>姓名:</label>
              <input type="text" v-model="editForm.name">
            </div>
            <div class="form-item">
              <label>角色:</label>
              <input type="text" v-model="editForm.role" required>
            </div>
            <div class="form-item">
              <label>部门:</label>
              <input type="text" v-model="editForm.department" required>
            </div>
            <div class="form-item">
              <label>状态:</label>
              <select v-model="editForm.status">
                <option value="1">正常</option>
                <option value="0">停用</option>
              </select>
            </div>
            <div class="form-item">
              <label>权限:</label>
              <textarea v-model="editForm.permissions" rows="3" required></textarea>
            </div>
          </div>
          <div class="modal-footer">
            <button class="modal-close-btn" @click="closeEditModal">取消</button>
            <button class="modal-save-btn" @click="updateAdminInfo" :disabled="saving">
              {{ saving ? '保存中...' : '保存' }}
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      // 搜索表单数据
      searchForm: {
        adminId: '',
        userId: '',
        status: ''
      },
      
      // 管理员列表数据
      allAdminList: [], // 存储完整数据
      adminList: [],    // 当前显示数据
      total: 0,
      currentPage: 1,
      pageSize: 5,
      maxVisiblePages: 5, // 最多显示的分页按钮数
      
      // 详情对话框
      showDetailModal: false,
      currentDetail: {},
      
      // 编辑对话框
      showEditModal: false,
      editForm: {
        adminId: '',
        name: '',
        role: '',
        department: '',
        status: '1',
        permissions: ''
      },
      saving: false
    };
  },
  
  computed: {
    // 计算总页数
    totalPages() {
      return Math.ceil(this.total / this.pageSize);
    },
    
    // 计算可见的分页按钮
    visiblePages() {
      const range = [];
      const start = Math.max(1, this.currentPage - Math.floor(this.maxVisiblePages / 2));
      const end = Math.min(this.totalPages, start + this.maxVisiblePages - 1);
      
      for (let i = start; i <= end; i++) {
        range.push(i);
      }
      
      return range;
    }
  },
  
  methods: {
    // 获取管理员列表
    async fetchAdminList() {
      try {
        console.log('开始获取管理员列表');
        const response = await axios.get('http://localhost:8080/admin/list');
        console.log('获取管理员列表成功，原始响应数据:', response);
        
        if (response.data && Array.isArray(response.data)) {
          this.allAdminList = response.data;
          this.total = response.data.length;
          this.applyPagination();
          console.log('处理后的管理员数据:', this.allAdminList);
        } else {
          console.error('获取管理员列表失败: 返回数据格式不正确');
          this.allAdminList = [];
          this.adminList = [];
          this.total = 0;
        }
      } catch (error) {
        console.error('获取管理员列表失败，详细错误信息:', {
          message: error.message,
          status: error.response?.status,
          data: error.response?.data,
          config: error.config
        });
        this.allAdminList = [];
        this.adminList = [];
        this.total = 0;
      }
    },
    
    // 应用分页
    applyPagination() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.adminList = this.allAdminList.slice(start, end);
    },
    
    // 跳转到指定页
    goToPage(page) {
      if (page < 1 || page > this.totalPages) return;
      this.currentPage = page;
      this.applyPagination();
    },
    
    // 搜索方法
    // 搜索方法
    async handleSearch() {
      try {
        console.log('开始搜索管理员，搜索条件:', this.searchForm);
        
        // 构建查询参数
        const params = {
          name: this.searchForm.name,
          role: this.searchForm.role,
          status: this.searchForm.status ? parseInt(this.searchForm.status) : null
        };
        
        const response = await axios.get('http://localhost:8080/admin/search', {
          params: params
        });
        
        console.log('搜索响应数据:', response);
        
        if (response.data && Array.isArray(response.data)) {
          this.allAdminList = response.data;
          this.total = response.data.length;
          this.currentPage = 1;
          this.applyPagination();
          console.log('搜索后的管理员数据:', this.allAdminList);
        } else {
          this.allAdminList = [];
          this.adminList = [];
          this.total = 0;
        }
      } catch (error) {
        console.error('搜索管理员失败，详细错误信息:', {
          message: error.message,
          status: error.response?.status,
          data: error.response?.data,
          config: error.config
        });
        alert('搜索失败: ' + (error.response?.data?.message || error.message));
      }
    },
    
    // 重置搜索表单
    resetSearch() {
      this.searchForm = {
        name: '',
        role: '',
        status: ''
      };
      this.fetchAdminList();
      this.currentPage = 1;
    },
    
    // 查看管理员详情
    showAdminDetail(admin) {
      this.currentDetail = {...admin};
      this.showDetailModal = true;
    },
    
    // 打开编辑模态框
    openEditModal(admin) {
      this.editForm = {
        adminId: admin.adminId,
        name: admin.name || '',
        role: admin.role,
        department: admin.department,
        status: admin.status.toString(),
        permissions: admin.permissions
      };
      this.showEditModal = true;
    },
    
    // 关闭编辑模态框
    closeEditModal() {
      this.showEditModal = false;
      this.saving = false;
    },
    
    // 更新管理员信息
    async updateAdminInfo() {
      // 表单验证
      if (!this.editForm.role || !this.editForm.department || !this.editForm.permissions) {
        alert('请填写所有必填字段');
        return;
      }
      
      try {
        this.saving = true;
        console.log('开始更新管理员信息:', this.editForm);
        
        const dataToSend = {
          adminId: this.editForm.adminId,
          name: this.editForm.name,
          role: this.editForm.role,
          department: this.editForm.department,
          status: parseInt(this.editForm.status),
          permissions: this.editForm.permissions
        };
        
        const response = await axios.put(
          `http://localhost:8080/admin/update/${this.editForm.adminId}`, 
          dataToSend
        );
        
        console.log('更新管理员信息响应:', response);
        
        if (response.data && response.data.status === "success") {
          alert('管理员信息更新成功！');
          
          // 更新本地数据
          const index = this.allAdminList.findIndex(a => a.adminId === this.editForm.adminId);
          if (index !== -1) {
            this.allAdminList[index] = {
              ...this.allAdminList[index],
              ...dataToSend
            };
            this.applyPagination();
          }
          
          this.closeEditModal();
        } else {
          alert('更新失败: ' + (response.data?.message || '未知错误'));
        }
      } catch (error) {
        console.error('更新管理员信息失败，详细错误信息:', {
          message: error.message,
          status: error.response?.status,
          data: error.response?.data,
          config: error.config
        });
        alert('更新失败: ' + (error.response?.data?.message || error.message));
      } finally {
        this.saving = false;
      }
    }
  },
  
  mounted() {
    this.fetchAdminList();
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

body {
  background: linear-gradient(135deg, #f5f9ff 0%, #eef2f7 100%);
  min-height: 100vh;
  color: #2c3e50;
}

.admin-container {
  display: flex;
  min-height: 100vh;
}

/* 主内容区样式 */
.main-content {
  flex: 1;
  padding: 30px;
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
}

.header {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid rgba(52, 152, 219, 0.15);
}

.header h1 {
  color: #2c3e50;
  font-size: 28px;
  font-weight: 700;
  display: flex;
  align-items: center;
}

.header h1 i {
  margin-right: 12px;
  color: #3498db;
  background: rgba(52, 152, 219, 0.1);
  padding: 10px;
  border-radius: 10px;
}

/* 搜索区域样式 */
.search-container {
  background: white;
  border-radius: 16px;
  padding: 25px;
  margin-bottom: 30px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.06);
  border: 1px solid #eef2f7;
}

.search-title {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 25px;
  color: #2c3e50;
  padding-bottom: 15px;
  border-bottom: 1px solid #eef2f7;
  display: flex;
  align-items: center;
}

.search-title i {
  margin-right: 10px;
  color: #3498db;
  background: rgba(52, 152, 219, 0.1);
  padding: 8px;
  border-radius: 8px;
}

.search-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.form-item {
  display: flex;
  flex-direction: column;
}

.form-item label {
  margin-bottom: 10px;
  color: #34495e;
  font-weight: 500;
  font-size: 14px;
  display: flex;
  align-items: center;
}

.form-item label i {
  margin-right: 8px;
  color: #7f8c8d;
  font-size: 14px;
}

.form-item input, .form-item select {
  padding: 12px 16px;
  border: 1px solid #e0e6ed;
  border-radius: 10px;
  font-size: 15px;
  transition: all 0.3s;
  background: #f8fafc;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.03);
}

.form-item input:focus, .form-item select:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.15);
  background: white;
}

.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  margin-top: 15px;
}

.search-btn, .reset-btn {
  padding: 0px 25px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s;
  border: none;
  font-size: 15px;
  display: flex;
  align-items: center;
}

.search-btn {
  background: linear-gradient(to right, #3498db, #2980b9);
  color: white;
  box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
}

.search-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
}

.search-btn i {
  margin-right: 8px;
}

.reset-btn {
  background: white;
  color: #7f8c8d;
  border: 1px solid #e0e6ed;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.reset-btn:hover {
  color: #3498db;
  border-color: #3498db;
  background-color: #f5f9ff;
}

.reset-btn i {
  margin-right: 8px;
}

/* 表格区域样式 */
.table-container {
  background: white;
  border-radius: 16px;
  padding: 25px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.06);
  border: 1px solid #eef2f7;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.table-header h3 {
  font-size: 20px;
  font-weight: 600;
  color: #2c3e50;
  display: flex;
  align-items: center;
}

.table-header h3 i {
  margin-right: 10px;
  color: #3498db;
  background: rgba(52, 152, 219, 0.1);
  padding: 8px;
  border-radius: 8px;
}

.table-total {
  color: #7f8c8d;
  font-size: 14px;
  background: #f8fafc;
  padding: 5px 12px;
  border-radius: 20px;
}

.admin-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.03);
}

.admin-table th {
  background: #f8fafc;
  padding: 16px 20px;
  text-align: left;
  color: #2c3e50;
  font-weight: 600;
  border-bottom: 2px solid #eef2f7;
}

.admin-table td {
  padding: 18px 20px;
  border-bottom: 1px solid #f1f5f9;
  color: #34495e;
  transition: all 0.3s;
}

.admin-table tr:not(:last-child) td {
  border-bottom: 1px solid #f1f5f9;
}

.admin-table tr:hover td {
  background: #f5f9ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(52, 152, 219, 0.1);
}

.action-buttons {
  display: flex;
  gap: 10px;
}

.action-btn {
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.3s;
  border: none;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}

.detail-btn {
  background: linear-gradient(to right, #206594, #216898);
  color: white;
  box-shadow: 0 3px 8px rgba(52, 152, 219, 0.2);
}

.detail-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 12px rgba(52, 152, 219, 0.3);
}

.detail-btn i {
  margin-right: 6px;
}

.edit-btn {
  background: linear-gradient(to right, #2e8dcc, #2797ae);
  box-shadow: 0 3px 8px rgba(46, 128, 204, 0.2);
}

.edit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 12px rgba(46, 130, 204, 0.3);
}

.edit-btn i {
  margin-right: 6px;
}

.status-active {
  color: #2766ae;
  font-weight: 500;
  display: flex;
  align-items: center;
}

.status-active::before {
  content: "";
  width: 10px;
  height: 10px;
  background: #2776ae;
  border-radius: 50%;
  margin-right: 8px;
  display: inline-block;
}

.status-inactive {
  color: #e74c3c;
  font-weight: 500;
  display: flex;
  align-items: center;
}

.status-inactive::before {
  content: "";
  width: 10px;
  height: 10px;
  background: #e74c3c;
  border-radius: 50%;
  margin-right: 8px;
  display: inline-block;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  gap: 8px;
}

.pagination button {
  padding: 8px 16px;
  border: 1px solid #e0e6ed;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: 500;
  min-width: 40px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.05);
}

.pagination button.active {
  background: #3498db;
  color: white;
  border-color: #3498db;
  box-shadow: 0 3px 8px rgba(52, 152, 219, 0.3);
}

.pagination button:hover:not(.active) {
  color: #3498db;
  border-color: #3498db;
  background: #f5f9ff;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  box-shadow: none;
}

.empty-data {
  text-align: center;
  padding: 60px 0;
  color: #a0aec0;
}

.empty-data i {
  font-size: 70px;
  margin-bottom: 20px;
  opacity: 0.2;
}

.empty-data p {
  font-size: 18px;
  color: #718096;
}

/* 模态框样式 */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.modal-content {
  background: white;
  border-radius: 16px;
  width: 500px;
  max-width: 90%;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
  animation: modalFadeIn 0.3s ease-out;
  overflow: hidden;
  border: 1px solid #eef2f7;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  padding: 20px 25px;
  background: #f8fafc;
  border-bottom: 1px solid #eef2f7;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  font-size: 20px;
  color: #2c3e50;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #a0aec0;
  transition: color 0.3s;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #e74c3c;
  background: rgba(231, 76, 60, 0.1);
}

.modal-body {
  padding: 25px;
  background: white;
}

.detail-item {
  margin-bottom: 18px;
  display: flex;
  align-items: flex-start;
}

.detail-item label {
  width: 100px;
  font-weight: 500;
  color: #4a5568;
  flex-shrink: 0;
}

.detail-item span {
  flex: 1;
  color: #2c3e50;
  word-break: break-word;
}

/* 编辑模态框样式 */
.modal-body .form-item {
  margin-bottom: 18px;
}

.modal-body .form-item label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #4a5568;
  font-size: 14px;
}

.modal-body .form-item input,
.modal-body .form-item select,
.modal-body .form-item textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #e0e6ed;
  border-radius: 10px;
  font-size: 15px;
  background: #f8fafc;
  transition: all 0.3s;
}

.modal-body .form-item textarea {
  min-height: 100px;
  resize: vertical;
}

.modal-body .form-item input:focus,
.modal-body .form-item select:focus,
.modal-body .form-item textarea:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.15);
  background: white;
}

.modal-footer {
  padding: 20px 25px;
  border-top: 1px solid #eef2f7;
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  background: #f8fafc;
}

.modal-close-btn {
  padding: 10px 22px;
  background: #e2e8f0;
  color: #4a5568;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: 500;
}

.modal-close-btn:hover {
  background: #cbd5e0;
}

.modal-save-btn {
  padding: 10px 22px;
  background: linear-gradient(to right, #2ecc71, #27ae60);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(46, 204, 113, 0.2);
}

.modal-save-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(46, 204, 113, 0.3);
}

.modal-save-btn:disabled {
  background: #a0aec0;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-content {
    padding: 20px;
  }
  
  .search-form {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .main-content {
    padding: 15px;
  }
  
  .header h1 {
    font-size: 24px;
  }
  
  .search-form {
    grid-template-columns: 1fr;
  }
  
  .table-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .admin-table {
    display: block;
    overflow-x: auto;
  }
  
  .action-buttons {
    flex-wrap: wrap;
  }
  
  .pagination {
    flex-wrap: wrap;
  }
  
  .modal-content {
    width: 95%;
  }
  
  .detail-item {
    flex-direction: column;
    gap: 5px;
  }
  
  .detail-item label {
    width: 100%;
  }
}
.el-card:hover{
  box-shadow: 0 4px 19px rgba(0, 0, 0, 0.25);
}
</style>